import $Util from './commonUtil.js'
import Vue from 'vue'

export default {
	generateModelAndRules: function(formItems, propFrefix = '', propEndFix = '', changeLabel = false) {
		var model = {};
		// 设置model
		formItems.forEach(n => {
			if (!n.prop) {
				return;
			}
			if (propEndFix) {
				n.prop = n.prop + propEndFix;
				n.propEndFix = propEndFix;
			}
			if (changeLabel) {
				n.label = n.label + propEndFix;
			}
			model[n.prop] = n.value || '';		
			
			if (n.optionApi &&  !n.lazySearch) {
                var vueApp = global.vueApp;
                n.optionApi = n.optionApi.replace('【openId】', uni.getStorageSync('openId'));
				vueApp.getAndLoad('读取option列表', n.optionApi, function (res) {
					n.options = res.data;
					if (n.type == 'picker') {
						n.columns = [n.options];
					}
				});
			} else if (n.options) {
				n.columns = [n.options];
			}
			if (n.type == 'checkbox') {
				if (!n.options.map) {
					n.options = n.options.split('$').map((m, i) => {
						return {
							name: m.split(':')[1],
							active: !i,
							value: m.split(':')[0],
						};
					})
				} else {
					n.options = n.options.map((o, i) => {
						return { name: o, active: !i };
					});
				}
				if (n.options[0]) {
					model[n.prop] = model[n.prop].value || model[n.prop].name;
				}
			}
			if (n.type == 'picker') {
				n.showPicker = false;
			}
			if (n.type == 'areapicker') {
				n.provinceCityArea = [];
			}
			if (n.type == 'image') {
				!n.limit && (n.limit = 1);
				n.fileList = [];
			}
			
			
		});

		// 设置rule
		var rules = {};
		formItems.forEach(item => {
			if (item.required) {
				rules[propFrefix + item.prop] = [{
					type: 'string',
					required: true,
					message: '此项为必填',
					trigger: ['blur', 'change']
				}];
			}
			if (item.validator) {
				if (!rules[propFrefix + item.prop]) {
					rules[propFrefix + item.prop] = [];
				}
				item.validator.forEach(n => {
					if (n == 'phone') {
						rules[propFrefix + item.prop].push({
							type: 'string',
							validator: (rule, value, callback) => {
								return (!item.required && !value) || uni.$u.test.mobile(
									value);
							},
							message: '手机号码不正确',
							trigger: ['change', 'blur']
						});
					} else if (n == 'idcard') {
						rules[propFrefix + item.prop].push({
							type: 'string',
							validator: (rule, value, callback) => {
								return !$Util.idCardValidator(value);
							},
							message: '身份证号码不正确',
							trigger: ['change', 'blur']
						});
					}
				});
			}
		});
		return {
			rules,
			model
		};
	},
	isDateInSection: function (value, section) {
		var valueMin = section[0];
		var valueMax = section[1];
		function getValue(v, type = 'min') {
			if (isNaN(v)) {
				// 写法  day*1+today
				var v2 = v.split('+');
				var total = 0;
				var todayStart = new Date().setHours(0, 0, 0, 0);
				var todayEnd = new Date().setHours(23, 59, 59, 999);
				v2.forEach(v3 => {
					if (v3 == 'today') {
						if (type == 'max') {
							total += (todayEnd - new Date().getTime());
						} else {
							total += (new Date().getTime() - todayStart);
						}
					} else if (v3.includes('*')) {
						var v4 = v3.split('*');
						var count = v4[1];
						var unit = v4[0];
						if (unit == 'day') {
							total += (count * 86400000);
						}
					}
				});
				return total;
			} else {
				return parseInt(v);
			}
		}
		console.log('最小时间：' + (new Date().getTime() - getValue(valueMin, 'min')));
		console.log('最大时间：' + (new Date().getTime() + getValue(valueMax, 'max')));
		console.log('比较时间：' + value);
		return value >= (new Date().getTime() - getValue(valueMin, 'min')) && (value <= new Date().getTime() + getValue(valueMax, 'max'));
	}
};
